import req from '../utils/request.js'

// 前后端数据交互都在此处
// 从本地中取数据
function getStorage(n) {
	let data = localStorage[n];
	return JSON.parse(data)
}
// 向本地中存数据
function setStorage(n, d) {
	localStorage[n] = JSON.stringify(d)
}

// 注册接口
export async function regiseter_api(obj) {
	try {
		 // return await req.post('/mock/regiseter');
		return await req.post('/member/register',obj);
	} catch {
		// 确认是否有 容器 接收数据
		if (!localStorage["users"]) {
			setStorage('users', [])
		}
		// 先将容器内所有的用户信息取出
		let arr = getStorage('users');
		// 将所有数据全循环比较一遍 确认没有重复的用户名
		for (let i = 0; i < arr.length; i++) {
			if (arr[i].userName == obj.userName) {
				return {
					code: 0,
					msg: '用户名重复，请输入新的用户名'
				}
			}
		}
		// 确认用户名没有重复后 将新的用户信息存到容器中
		arr.push(obj);
		setStorage('users', arr);
		// 给用户反馈
		return {
			code: 1,
			msg: '注册成功'
		}
	}

}

// 登录接口
export async function login_api(obj) {
	try {
		// let res = await req.post('/mock/login');
		let res =  await req.post('/member/login',obj);
		if (res.code) {
			localStorage['un'] = obj.userName;
		}
		return res
	} catch {
		// 先确认是否有数据
		if (!localStorage['users']) {
			setStorage('users', []);
		}
		let arr = getStorage('users');
		let flag = true;
		// 对比 输入的用户名是否跟存的用户名一样
		for (let i = 0; i < arr.length; i++) {
			if (arr[i].userName == obj.userName) {
				flag = false;
				// 用户名正确 开始判断密码是否正确
				if (arr[i].passWord == obj.passWord) {
					localStorage['un'] = obj.userName;
					return {
						code: 1,
						msg: '登录成功'
					}
				} else {
					return {
						code: 0,
						msg: '密码错误'
					}
				}
			}
		}

		if (flag) {
			return {
				code: 0,
				msg: '用户名错误'
			}
		}
	}
}


// 首页推荐接口
export async function homeSell() {
	try {
		// return await req.post('/mock/homeSell')
		return await req.post('/stock/homeSell')
	} catch {
		return await new Promise((resolve, reject) => {
			uni.request({
				url: '/mock/homeSell',
				method: 'POST',
				header: {
					"Content-Type": "application/json"
				},
				success: (data) => {
					//做响应拦截
					resolve(data.data)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	}
}

// 请求策略推荐左侧列表接口
export async function leftList() {
	localStorage.removeItem("leftList");
	//如果服务器有数据，就将数据进行同步
	let obj = await req.post("/stock/leftList");
	if(obj.code==1 && obj.data.length>0){
		obj.data.unshift({
			"id": '0',
			"name": 'BIAS乖离率推荐',
			"table": [{
				"label": "股票代码",
				"prop": "stockcode",
				"sortable": false,
			}, {
				"label": "股票名",
				"sortable": false,
				"prop": "stockname", 
			}, {
				"label": "交易日期",
				"sortable": true,
				"prop": "tradedate",
			}, {
				"label": "收盘价",
				"sortable": true,
				"prop": "stockclose",
			}, {
				"label": "乖离率",
				"sortable": true,
				"prop": "bias",
			}],
		});
		setStorage('leftList',obj.data);
	}
	if (!localStorage['leftList']) {
		setStorage('leftList', [{
			"id": '0',
			"name": 'BIAS乖离率推荐',
			"table": [{
				"label": "股票代码",
				"prop": "stockcode",
				"sortable": false,
			}, {
				"label": "股票名",
				"sortable": false,
				"prop": "stockname",
			}, {
				"label": "交易日期",
				"sortable": true,
				"prop": "tradedate",
			}, {
				"label": "收盘价",
				"sortable": true,
				"prop": "stockclose",
			}, {
				"label": "乖离率",
				"sortable": true,
				"prop": "bias",
			}],
		}]);
	}
	let arr = getStorage('leftList');
	return arr;
}


// 策略推荐数据接口
export async function sellData(obj) {
	try {
		return await req.post('/stock/gll', obj)
	} catch {
		return await new Promise((resolve, reject) => {
			uni.request({
				url: '/mock/gll',
				method: 'POST',
				header: {
					"Content-Type": "application/json"
				},
				success: (data) => {
					//做响应拦截
					resolve(data.data)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	}
}

// 策略研究提交接口
export async function sendSell(obj) {
	return await req.post('/stock/sendSell', obj);
}

// 策略推荐列表新增接口
export async function sendList(obj) {
	if (!localStorage['leftList']) {
		setStorage('leftList', [{
			"id": '0',
			"name": 'BIAS乖离率推荐',
			"table": [{
				"label": "股票代码",
				"prop": "stockcode",
				"sortable": false,
			}, {
				"label": "股票名",
				"sortable": false,
				"prop": "stockname",
			}, {
				"label": "交易日期",
				"sortable": true,
				"prop": "tradedate",
			}, {
				"label": "收盘价",
				"sortable": true,
				"prop": "stockclose",
			}, {
				"label": "乖离率",
				"sortable": true,
				"prop": "bias",
			}],
		}]);
	}
	let arr = getStorage('leftList');
	//let id_ = Math.round(Math.random() * 899999 + 100000);
	arr.push({
		id: obj.id,
		name: obj.name,
		table: obj.table
	});
	console.log(arr);
	setStorage('leftList', arr);
	return await req.post('/stock/sendList', obj);
}


// 请求股票图 id为股票代码
export async function sendSrc(id) {
	try {
		return await req.post('/mock/src', {
			stockCode: id
		});
	} catch {
		return await new Promise((resolve, reject) => {
			uni.request({
				data: {
					stockCode: id
				},
				url: '/mock/src',
				method: 'POST',
				header: {
					"Content-Type": "application/json"
				},
				success: (data) => {
					//做响应拦截
					resolve(data.data)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	}
}
// 请求股票图数据
export async function sendImgData(code) {
	return await req.post('/stock/imgData', {
		code
	});
}